package com.atguigu.mybatis.test;

import com.atguigu.mybatis.entity.Tiger;
import com.atguigu.mybatis.mapper.TigerMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class DemoTest01 {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private TigerMapper tigerMapper;

    @Test
    public void test01() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);
    }

    @Test
    public void test02() {
        Tiger tiger = new Tiger(null, "tigerMybatis01", 111, 111.11);

        tigerMapper.insertTiger(tiger);
    }

    @Test
    public void test03() {
        tigerMapper.deleteTigerById(10012);
    }

    @Test
    public void test04() {
        Tiger tiger = new Tiger(20026, "tigerMybatis01~~~", 111, 111.11);
        tigerMapper.updateTigerById(tiger);
    }

    @Test
    public void test05() {
        Tiger tiger = tigerMapper.selectTigerById(20026);
        System.out.println("tiger = " + tiger);
    }

    @Test
    public void test06() {
        List<Tiger> tigerList = tigerMapper.selectTigerList();
        for (Tiger tiger : tigerList) {
            System.out.println("tiger = " + tiger);
        }
    }

    @Test
    public void test07() {
        String tableName = "t_dog";
        tigerMapper.createTable(tableName);
    }

    @Test
    public void test08() {
        String tigerName = "tigerMybatis03";
        int tigerAge = 3333;
        double tigerSalary = 333.33;
        tigerMapper.insertTigerBySeveralValue(tigerName, tigerAge, tigerSalary);
    }

    @Test
    public void test09() {
        Map<String, Object> map = new HashMap<>();
        map.put("tigerNameKey", "tigerMybatis04");
        map.put("tigerAgeKey", 66666);
        map.put("tigerSalaryKey", 666666.66);

        tigerMapper.insertTigerByMap(map);
    }

    @Test
    public void test10() {
        double avgSalary = tigerMapper.selectAvgSalary();
        System.out.println("avgSalary = " + avgSalary);
    }

    @Test
    public void test11() {
        int tigerId = 20025;
        // Map 的 key：数据库表中字段的名称
        // Map 的 Value：查询结果中一条记录里面，字段对应的值
        // 一个 Map 只能封装一条记录
        Map<String, Object> map = tigerMapper.selectTigerForMap(tigerId);
        for (String key : map.keySet()) {
            Object value = map.get(key);
            System.out.println(key + " = " + value);
        }
    }

    @Test
    public void test12() {
        List<Map<String, Object>> mapList = tigerMapper.selectAllForMap();
        for (Map<String, Object> map : mapList) {
            for (String key : map.keySet()) {
                Object value = map.get(key);
                System.out.println(key + " = " + value);
            }
            System.out.println("---------------------------");
        }
    }

    @Test
    public void test13() {
        Tiger tiger = new Tiger(null, "tigerMybatis06", 66666, 6666.66);

        // 方法返回值是受影响的行数
        int effectedRows = tigerMapper.insertTigerWithAutoPK(tiger);
        System.out.println("effectedRows = " + effectedRows);

        // 通过实体类对象来获取自增主键的值
        Integer tigerId = tiger.getTigerId();
        System.out.println("tigerId = " + tigerId);
    }

    @Test
    public void test14() {
        List<Tiger> tigerList = tigerMapper.selectTigerListUseResultMap();
        for (Tiger tiger : tigerList) {
            System.out.println("tiger = " + tiger);
        }
    }
}
